package com.zdy.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * @Author: Deyou
 * @Date: 2021/4/12
 */
@Controller
@ResponseBody
public class FileuploadController {
    @PostMapping("/fileupload")
    public String fileUpload(HttpServletRequest request, @RequestParam("file") MultipartFile file){
        if(file.isEmpty()) return "failure";
        //上传文件路径
        String path = request.getServletContext().getRealPath("/uploadfile/");
        //上传文件名
        String filename = file.getOriginalFilename();
        File filePath = new File(path,filename);
        //判断父文件是否存在，如果不存在就创建一个
        if(!filePath.getParentFile().exists()){
            filePath.getParentFile().mkdirs();
        }
        try {
            file.transferTo(new File(path+File.separator+filename));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "success";
    }
    @PostMapping("/fileuploads")
    public String fileUpload2(HttpServletRequest request, @RequestParam("files") MultipartFile[] files){
        if(files.length == 0) return "failure";

        for (int i = 0; i < files.length; i++) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/uploadfile/");
            //上传文件名
            String filename = files[i].getOriginalFilename();
            File filePath = new File(path,filename);
            //判断父文件是否存在，如果不存在就创建一个
            if(!filePath.getParentFile().exists()){
                filePath.getParentFile().mkdirs();
            }
            try {
                files[i].transferTo(new File(path+File.separator+filename));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return "success";

    }

    // 文件下载
    @RequestMapping(value = "/download")
    public ResponseEntity<byte[]> download(HttpServletRequest request, @RequestParam("filename") String filename) throws Exception{
        // 下载路径
        String path = request.getServletContext().getRealPath("/uploadfile/");
        File file = new File(path+File.separator+filename);
        HttpHeaders headers = new HttpHeaders();
        // 解决中文乱码
        String downloadfile =  new String(filename.getBytes("UTF-8"),"iso-8859-1");
        // 以下载方式打开文件
        headers.setContentDispositionFormData("attachment", downloadfile);
        // 二进制流
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);

        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);

    }
}
